Ext.define('Admin.view.role.RoleViewController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.roleViewController',
    	
    search: function(view, recIndex, cellIndex, item, e, record) {
		var searchValue = this.lookupReference('searchFieldValue').getValue();
		
		var store = Ext.getCmp('roleGridPanel').getStore();
		
		Ext.apply(store.proxy.extraParams, {roleName:searchValue});
		
		store.load({params:{start:0, limit:25, page:1}});
		
    }
	,openMoreSearchWindow: function(view, recIndex, cellIndex, item, e, record) {
      		alert("open  RoleGridPanel  MoreSearchWindow");
    }
    ,moreSearch: function(view, recIndex, cellIndex, item, e, record) {
      		alert("roleGridPanel submit MoreSearchForm");
    }
    ,openRolePerSelWindow: function(view, recIndex, cellIndex, item, e, record) {
          var cfg = Ext.apply({
                      xtype: 'rolePerWindow',
                      items: [Ext.apply({xtype: 'roleRolePerForm'})]
          });
          var win = Ext.create(cfg);
          var formItem1 = Ext.getCmp('roleperform1');
          var formItem2 = Ext.getCmp('roleperform2');
          Ext.Ajax.request({  
                url : 'role/findPer',  
                params : {roleId : record.data.roleId},  //携带的参数           
                method : 'POST',  
                success : function(response, opts) {
                  var list=Ext.decode(response.responseText)||'';
                  for(var i=0;i<Object.keys(list).length;i++){
                  var configItem=[
                        {
                            boxLabel  : list[i].permissionName,
                            name      : 'permissionId',
                            checked:    list[i].checked,
                            inputValue: list[i].permissionId,
                            id        : 'checkbox'+i
                        }
                    ];
                if(list[i].permissionType=='页面权限'){
                formItem1.add(configItem[0]);}else{
                  formItem2.add(configItem[0]);
                }
            }
                  },  
                      failure : function(response, opts) { }  
              });
          win.down("form").getForm().loadRecord(record);
    }
    ,openAddWindow: function(view, recIndex, cellIndex, item, e, record) {
      		
  	var cfg = Ext.apply({
                xtype: 'roleAddWindow',
                items: [Ext.apply({xtype: 'roleAddForm'})]
    });
    var win = Ext.create(cfg);
    }
    ,submitAddForm: function() {
      	var cfg = Ext.apply({
                xtype: 'roleEditWindow',
                items: [Ext.apply({xtype: 'roleAddForm'})]
		});
		var win = Ext.create(cfg);
    }
    ,openEditWindow: function(view, recIndex, cellIndex, item, e, record) {
    	var cfg = Ext.apply({
                xtype: 'roleEditWindow',
                items: [Ext.apply({xtype: 'roleEditForm'})]
		});
		var win = Ext.create(cfg);
		win.down("form").getForm().loadRecord(record);//加载修改数据到Form
    }
     ,submitEditForm: function(btn) {
      		var form = btn.up('form');
			form.getForm().submit({       
				url:'role/saveOrUpdate',
				//waitMsg: '正在上传，请耐心等待....',
				success: function(view, action){    
					Ext.Msg.alert('提示', action.result.msg,function(){
						btn.up('window').close();
						//form.getViewModel().getStore('roleStore').reload();
	                    Ext.getCmp('roleGridPanel').getStore().reload();
					});       
				},       
				failure: function(view, action){
					Ext.Msg.alert('提示', action.result.msg);
				}
			});
    }
    ,submitSavePer: function(btn) {
          var form = btn.up('form');
      form.getForm().submit({       
        url:'role/savePer',
        // params : { 
        //       //ids[] :selectIds
        //       roleId:roleId,
        //       ids :obj
        //     },
        //waitMsg: '正在上传，请耐心等待....',
        success: function(view, action){    
          Ext.Msg.alert('提示', action.result.msg,function(){
            btn.up('window').close();
            //form.getViewModel().getStore('roleStore').reload();
                      //Ext.getCmp('roleGridPanel').getStore().reload();
          });       
        },       
        failure: function(view, action){
          Ext.Msg.alert('提示', action.result.msg);
        }
      });
    }
    ,deleteRole: function(view, recIndex, cellIndex, item, e, record) {
      		Ext.MessageBox.confirm('提示', '确定要进行删除操作吗？数据将无法还原！',
      			function(btn, text){
                	if(btn=='yes'){
                			Ext.Ajax.request({ 
								url : 'role/delete', 
								method : 'post', 
								params : { 
									roleId :record.get('roleId')
								}, 
								success: function(response, options) {
					                var json = Ext.util.JSON.decode(response.responseText);
						            if(json.success){
						            	Ext.Msg.alert('系统提示', json.msg, function() {
						                    view.getStore().reload();
						                });
							        }else{
							        	 Ext.Msg.alert('系统提示', json.msg);
							        }
					            }
							});
               		}
            	}
            , this);
    },
    deleteRoles: function(btn) {
      	var grid = btn.up('gridpanel');
		var selModel = grid.getSelectionModel();
        if (selModel.hasSelection()) {
            Ext.Msg.confirm("警告", "确定要删除吗？", function (button) {
                if (button == "yes") {
                    var rows = selModel.getSelection();
                    var selectIds = []; //要删除的id
                    Ext.each(rows, function (row) {
                        selectIds.push(row.data.roleId);
                    });
                  	Ext.Ajax.request({ 
						url : 'role/deleteRoles', 
						method : 'post', 
						params : { 
							//ids[] :selectIds
							ids :selectIds
						}, 
						success: function(response, options) {
			                var json = Ext.util.JSON.decode(response.responseText);
				            if(json.success){
				            	Ext.Msg.alert('操作成功', json.msg, function() {
				                    grid.getStore().reload();
				                });
					        }else{
					        	 Ext.Msg.alert('操作失败', json.msg);
					        }
			            }
					});

                }
            });
        }
        else {
            Ext.Msg.alert("错误", "没有任何行被选中，无法进行删除操作！");
        }
    }
    
});